package com.ly.mpayment.persist.service.slow;

import com.ly.mpayment.core.util.BigUtil;
import com.ly.mpayment.core.util.StringUtils;
import com.ly.mpayment.persist.entity.slow.SlowProduct;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Service
public class SlowProductServiceImpl extends AbstractService<SlowProduct,Serializable> implements SlowProductService {

	@Override
	public Page<SlowProduct> findPageProduct(Map<String, Object> parameters, int start, int pageSize) {
		String hql = "select c from SlowProduct c where 1 = 1 #[ and c.price = :price ]#  #[ and c.operator = :operator ]#   ";
		hql += " order by c.createTime desc ";
		return this.findPageByNamed(hql, parameters, start, pageSize);
	}

	@Override
	public boolean saveProduct(SlowProduct order) {
		if(order != null){
			this.saveOrUpdateData(order);
			return true;
		}
		return false;
	}

	@Override
	public boolean updateProduct(SlowProduct order) {
		if(order != null){
			this.updateEntity(order);
			return true;
		}
		return false;
	}

	@Override
	public SlowProduct findById(String id) {
		return this.findOneObject("select c from SlowProduct c where c.proId = ?", id);
	}

	@Override
	public boolean updateState(SlowProduct order) {
		if(order != null){
			this.updateEntity(order);
			return true;
		}
		return false;
	}

	@Override
	public List<SlowProduct> findAll(String operator) {
		return this.find(" select c from SlowProduct c where 1 = 1 "+ (StringUtils.isBlank(operator) ? "" : " and c.operator = '"+operator.trim()+"'") +" order by c.price ");
	}

	/**
	 * total_fee 折扣金额
	 * rechargeMoney 充值金额
	 */
	@Override
	public SlowProduct findBydiscountAmount(String total_fee,String rechargeMoney,String operator) {
		return this.findOneObject("select c  from  SlowProduct c where c.status = 1 and c.discountAmount = ? and c.price = ? and c.operator = ?", BigUtil.getBigDecimal(total_fee),rechargeMoney,operator);
	}

}
